package com.xuecheng.system.web.interceptor;

import com.alibaba.fastjson.JSON;
import com.xuecheng.commons.model.vo.AuthInfo;
import com.xuecheng.commons.utils.AuthInfoHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class AuthInfoInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws UnsupportedEncodingException {
        String payload = request.getHeader("payload");
        if (StringUtils.isEmpty(payload)){
            return true;
        }
        String json = URLDecoder.decode(payload, "utf-8");
        AuthInfo info = JSON.parseObject(json, AuthInfo.class);//info包不一定对
        AuthInfoHolder.setAuthInfo(info);
        return true;
    }
}
